/**
 * 
 */
package com.edocbase.desktop.swt.widgets;

import java.io.File;

import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeViewerListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeExpansionEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;

/**
 * @author X61
 * 
 */
public class FileExplorer implements ITreeViewerListener,
		ISelectionChangedListener {
	private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger
			.getLogger(FileExplorer.class);
	Composite parent = null;
	int style = 0;
	TreeViewer treeViewer = null;
	private ExplorerContentProvider contentProvider;

	public FileExplorer(Composite parent, int style) {
		super();
		this.parent = parent;
		this.style = style;
		createExplorer();
	}

	private void createExplorer() {
		if (isCheckboxTree()) {
			treeViewer = new CheckboxTreeViewer(parent, style | SWT.VIRTUAL);
		} else {
			treeViewer = new TreeViewer(parent, style | SWT.VIRTUAL);
		}
		contentProvider = new ExplorerContentProvider();
		treeViewer.setContentProvider(contentProvider);
		treeViewer.setLabelProvider(new FileLabelProvider());
		treeViewer.addTreeListener(this);
		treeViewer.addSelectionChangedListener(this);
	}

	public Tree getControl() {
		return treeViewer.getTree();
	}

	public boolean isCheckboxTree() {
		return (this.style & SWT.CHECK) > 0;
	}

	public void setInput(File file) {
		treeViewer.setInput(new FileNode(file));
	}

	@Override
	public void treeCollapsed(TreeExpansionEvent event) {
		// TODO Auto-generated method stub

	}

	@Override
	public void treeExpanded(TreeExpansionEvent event) {
		FileNode node = (FileNode) event.getElement();
		// contentProvider.updateChildCount(node, -1);
	}

	@Override
	public void selectionChanged(SelectionChangedEvent event) {
		IStructuredSelection selection = (IStructuredSelection) event
				.getSelection();
		Object obj = selection.getFirstElement();
		if (logger.isDebugEnabled()) {
			logger.debug("" + obj + " p "
					+ event.getSelectionProvider().getClass());
		}

	}
}
